package cn.eports.learn.web.servlet;

import cn.eports.learn.domain.User;
import cn.eports.learn.service.UserService;
import cn.eports.learn.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userListServlet")
public class UserListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
//        1.调用userListService完成调查
        UserService service = new UserServiceImpl();
        final List<User> users = service.findAll();
/**
 *         测试代码
 *         for (User user : users) {
 *             System.out.println("++++++++++++++++++++++++++++++++++++++");
 *             System.out.println(user.toString());
 *         }
 *         System.out.println("++++++++++++++++++++++++++++++++++++++");
 */

//        2.将list存入request域
        req.setAttribute("users",users);
//        3.转发list.jsp
        req.getRequestDispatcher("/list.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

